package com.aijk.ehm.sys.service;

import com.aijk.base.entity.PagingRequest;
import com.aijk.commons.utils.Utils;
import com.aijk.ehm.sys.model.SysModuleExample;
import com.aijk.ehm.sys.model.SysModule;
import com.aijk.ehm.sys.model.qo.ModuleQo;
import com.aijk.ehm.sys.mapper.SysModuleMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service()
public class SysModuleService {

    private static Logger logger = LoggerFactory.getLogger(SysModuleService.class);

    @Autowired
    private SysModuleMapper mapper;


    private SysModuleExample getExample(ModuleQo qo) {
        SysModuleExample example = new SysModuleExample();

        if (Utils.isNotEmpty(qo.getSortway())) example.setOrderByClause(Utils.toString(qo.getSortway()));
        if (qo != null ) {
            if(qo.getOr() != null&&qo.getOr()&&Utils.isNotEmpty(qo.getPid())&&Utils.isNotEmpty(qo.getId())){
                SysModuleExample.Criteria criteriaOr1=example.createCriteria();
                if (Utils.isNotEmpty(qo.getPid())) criteriaOr1.andPidEqualTo(Utils.toInteger(qo.getPid()));
                if (Utils.isNotEmpty(qo.getTitle())) criteriaOr1.andTitleEqualTo(Utils.toString(qo.getTitle()));

                SysModuleExample.Criteria criteria2=example.createCriteria();
                if (Utils.isNotEmpty(qo.getId())) criteria2.andIdEqualTo(Utils.toInteger(qo.getId()));
                if (Utils.isNotEmpty(qo.getTitle())) criteria2.andTitleEqualTo(Utils.toString(qo.getTitle()));
                
                example.or(criteria2);
             /*   example.or().andPidEqualTo(Utils.toInteger(qo.getPid()));
                example.or().andIdEqualTo(Utils.toInteger(qo.getId()));*/
            }else {
                SysModuleExample.Criteria criteria = example.createCriteria();
                if (Utils.isNotEmpty(qo.getPid())) criteria.andPidEqualTo(qo.getPid());
                if (Utils.isNotEmpty(qo.getTitle())) criteria.andTitleEqualTo(qo.getTitle());
                if (Utils.isNotEmpty(qo.getIds())) criteria.andIdIn(qo.getIds());
                if (Utils.isNotEmpty(qo.getSysFlag())) criteria.andSysFlagEqualTo(qo.getSysFlag());
                if (Utils.isNotEmpty(qo.getCategory())) criteria.andCategoryEqualTo(qo.getCategory());

            }

        }

        return example;
    }

    public Integer count(ModuleQo qo) {
        return mapper.countByExample(getExample(qo));
    }

    public List<SysModule> find(ModuleQo qo) {
        return mapper.selectByExample(getExample(qo));
    }

    public PageInfo<SysModule> pageList(PagingRequest pagingRequest, ModuleQo qo) {
        PageHelper.startPage(pagingRequest.getPageNum(), pagingRequest.getPageSize());
        return new PageInfo<SysModule>(mapper.selectByExample(getExample(qo)));
    }
    public SysModule findOne(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    public Integer update(SysModule record) {
        return mapper.updateByPrimaryKeySelective(record);
    }

    public Integer save(SysModule record) {
        return mapper.insert(record);
    }

    public Integer delete(Integer id) {
        return mapper.deleteByPrimaryKey(id);
    }



}
